package bsc.war.ajax;

import bsc.ejb.entity.Attachment;
import bsc.ejb.entity.Note;
import bsc.ejb.entity.ScoreCard;
import bsc.ejb.metadata.NoteType;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import java.io.File;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author memozac
 */
public class DeleteNote extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");

            Note note = delegate.findNote(Long.parseLong(request.getParameter("note")));
            processRequestInternal(request, response, delegate, note);
            
        } catch (Exception e) {
        }
        return null;
    }
    public static void processRequestInternal(HttpServletRequest request, HttpServletResponse response, DelegateBSC delegate, Note note){
        
        List<Attachment> attachments = delegate.findAttachment(note);
        for (Attachment attachment : attachments){
            try {
                FileUtils.forceDelete( new File(attachment.getAttachment()));
            } catch (Exception e) {}
            delegate.deleteAttachment(attachment);
        }
        delegate.deleteNote(note);
    }
}
